// 李忠义的代码
var basic = "http://test.cn/message/";



// 点击头部消息进入页面,获取状态码
let xx_status = window.location.search;
xx_status = xx_status.split("=")[1]

// 获取用户信息
var nav_right = new Vue({
    el: ".nav-right",
    data: {
        user_name:'',
        user_account:''
    },
    created: function() {
        $.ajax({
            url: "http://test.cn/user/getUserInfo",
            type: "GET",
            success: function(e) {
                nav_right.user_account=e.data[0].account;
                nav_right.user_name=e.data[0].name;
            }
        })
    }
})
// 未读消息
var no_read = new Vue({
    el: ".no-read",
    data: {
        noneRead: 0,
        status: ''
    },
    methods: {
        // 根据状态请求不同未读信息
        get_diff_weidu: function(url) {
            $.ajax({
                url: basic + url,
                success: function(e) {
                    fun_list._update_data(e.data);
                    $(".all-read").removeClass("read-change");
                    $(".no-read").addClass("read-change");
                }
            })
        },
        show_noRead: function() {
            switch (this.status) {
                case 0:
                    this.get_diff_weidu("linknoread");
                    break;
                case 1:
                    this.get_diff_weidu("integralnoread");
                    break;
                case 2:
                    this.get_diff_weidu("auditnoread");
                    break;
                case 3:
                    this.get_diff_weidu("feedbcaknoread");
            }
        }
    }
})
// 全部消息
var all_read = new Vue({
    el: ".all-read",
    data: {
        allRead: 0,
        status: ''
    },
    methods: {
        // 根据状态请求不同未读信息
        get_diff_weidu: function(url) {
            $.ajax({
                url: basic + url,
                success: function(e) {
                    fun_list._update_data(e.data);
                    $(".no-read").removeClass("read-change");
                    $(".all-read").addClass("read-change");
                }
            })
        },
        show_allRead: function() {
            switch (this.status) {
                case 0:
                    this.get_diff_weidu("link");
                    break;
                case 1:
                    this.get_diff_weidu("integral");
                    break;
                case 2:
                    this.get_diff_weidu("audit");
                    break;
                case 3:
                    this.get_diff_weidu("feedbcak");
            }
        }
    }
})

//左功能
var fun_list = new Vue({
    el: ".fun-list",
    data: {
        link: "link",
        integral: "integral",
        audit: "audit",
        feedbcak: "feedbcak"
    },
    mounted:function(){
        this.js_focus(0,$(".li-focus")[0]);
    },
    methods: {
        // 点击功能栏
        js_focus: function(status, event) {
            let item = event.target;
            switch (status) {
                case 0:
                    this._update_all(status, this.link, item);
                    break;
                case 1:
                    this._update_all(status, this.integral, item);
                    break;
                case 2:
                    this._update_all(status, this.audit, item);
                    break;
                case 3:
                    this._update_all(status, this.feedbcak, item);
                    break;
            }
        },
        // 修改背景
        _modify_background: function(item) {
            let current_item = item;
            $(current_item).addClass("modify-kackground");
            $(current_item).nextAll().removeClass("modify-kackground");
            $(current_item).prevAll().removeClass("modify-kackground");
        },
        // 更新未读数目
        _update_noRead: function(status) {
            $.ajax({
                url: basic + "noreadnum",
                data: {
                    status: status
                },
                success: function(e) {
                    no_read.noneRead = e.data;
                    no_read.status = status;
                }
            })
        },
        // 更新全部数目
        _update_allRead: function(status) {
            $.ajax({
                url: basic + "allnum",
                data: {
                    status: status
                },
                success: function(e) {
                    all_read.allRead = e.data;
                    all_read.status = status;
                }
            })
        },
        _update_data: function(e) {
            js_list.list_data = e.data;
            paging.current_page = e.current_page;
            paging.per_page = e.per_page;
            paging.last_page_url = e.last_page_url;
            paging.prev_page_url = e.prev_page_url;
            paging.next_page_url = e.next_page_url;
            paging.path = e.path;
            paging.first_page_url = e.first_page_url;
        },
        _update_all: function(status, url, item) {
            let url_link = url;
            let current_item = item;
            this._update_noRead(status);
            this._update_allRead(status);
            this._modify_background(current_item);
            $.ajax({
                url: basic + url_link,
                success: function(e) {
                    // 更新list的数据
                    fun_list._update_data(e.data);
                }
            })
        }
    }
})


// 渲染列表
var js_list = new Vue({
    el: ".js-list",
    data: {
        list_data: '', //列表数据
    },
    methods: {
        // 分割时间
        slice_time: function(e) {
            return e.slice(0, 11);
        },
        // 弹出反馈信息
        show_feedbcak: function(event) {
            $(".feedbcak-content").toggleClass("block");
            // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!存在一个问题
            $(".drop-list").css("background-image", "url('../../share/public/img/arrow-rise.png')");
        },
        // 检查反馈信息是否存在
        is_feedbacak: function(feedbcak) {
            if (feedbcak == null) {
                return "none"
            }
        },
        // 切换已读，未读状态
        chage_status: function(event, item) {
            if (item.readstatus == 0) {
                $.ajax({
                    url: basic + "switchread",
                    data: {
                        id: item.id
                    },
                    success: function(e) {
                        event.target.innerText = "已读";
                        event.target.disabled = "disabled";
                        event.target.style.backgroundColor = "#A9A9A9";
                    }
                })
            }
        },
        // 检查是否为已读
        is_read: function(readstatus) {
            if (readstatus == 1) {
                return "modify_bgr";
            }
        }
    }
})

// 分页功能
var paging = new Vue({
    el: ".paging",
    data: {
        per_page: 0, //总页面数
        current_page: 0, //当前页面
        first_page_url: '', //首页
        last_page_url: '', //尾页
        prev_page_url: '', //前一页
        next_page_url: '', //后一页
        init_page: '', //最前一页
        path: '' //跳转页面
    },
    methods: {
        // 首页
        init: function(e) {
            if (this.current_page != 1 && this.current_page != "") {
                $.ajax({
                    url: this.first_page_url,
                    success: function(e) {
                        console.log(e)
                        // 更新数据
                        fun_list._update_data(e.data);
                    }
                })
            } else {
                console.log("已是首页")
            }
        },
        // 前一页
        pre: function(e) {
            // 判断上一页是否存在
            if (this.prev_page_url != null) {
                // 如果从最后一页往前跳，恢复下一页样式
                if (this.current_page == this.per_page) {
                    this._recovery_next_btn();
                };
                $.ajax({
                    url: this.prev_page_url,
                    success: function(e) {
                        // 判断是否请求成功
                        if (e.code == 200) {
                            // 更新list的数据
                            fun_list._update_data(e.data);
                        } else {
                            console.log("请求失败哟~")
                        }
                    }
                })
            } else {
                console.log("已是首页");
                e.target.style.color = "gray";
                e.target.disabled = "disabled";
            }
        },
        // 后一页
        next: function(e) {
            // 判断下一页是否存在
            if (this.next_page_url != null) {
                // 判断前一页是否为空
                if (this.prev_page_url == null) {
                    // 恢复前一页样式
                    this._recovery_prev_btn();
                }
                $.ajax({
                    url: this.next_page_url,
                    success: function(e) {
                        // 判断是否请求成功
                        if (e.code == 200) {
                            // 更新list的数据
                            fun_list._update_data(e.data);
                        } else {
                            console.log("请求失败哟~")
                        }
                    }
                })
            } else {
                console.log("已是尾页");
                e.target.style.color = "gray";
                e.target.disabled = "disabled";
            }
        },
        // 末页
        end: function(e) {
            if (this.current_page != this.per_page) {
                // 下一页不可用
                this._ban_next_btn();

                $.ajax({
                    url: this.last_page_url,
                    success: function(e) {
                        // 更新数据
                        fun_list._update_data(e.data);
                    }
                })
            } else {
                console.log("已是尾页")
            }
        },
        // 跳转页
        jump: function(e) {
            let junm_num = $("#jump-btn").val();
            if (junm_num > 0 && junm_num < this.per_page + 1 && junm_num != this.current_page) {
                $.ajax({
                    url: this.path + "?page=" + junm_num,
                    success: function(e) {
                        // 更新list的数据
                        fun_list._update_data(e.data);
                    }
                })
            } else {
                console.log("输入的页数有误");
            }
        },
        // 下一页不可用
        _ban_next_btn: function() {
            let next_btn = $(".next-btn")[0];
            next_btn.style.color = "gray";
            next_btn.disabled = "disabled";
        },
        // 下一页不可用恢复
        _recovery_next_btn: function() {
            let next_btn = $(".next-btn")[0];
            next_btn.style.color = "white";
            next_btn.disabled = false;
        },
        // 前一页不可用恢复
        _recovery_prev_btn: function() {
            let prev_btn = $(".pre-btn")[0];
            prev_btn.style.color = "white";
            prev_btn.disabled = false;
        }
    }
})

// 搜索功能
var nav_form = new Vue({
    el: ".nav-form",
    methods: {
        bigsearch: function() {
            let content = $(".nav-form input").val().trim();
            if (content != '') {
                $.ajax({
                    type: 'get',
                    url: "http://test.cn/post/search",
                    data: {
                        keyword: content
                    },
                    success: function(data) {
                        if (data.code == 200) {
                            if (data.data == "对不起，没有相关文章") {
                                alert("对不起，没有相关文章！")
                                return 0;
                            } else {
                                window.open("http://test.cn/search-page?content=" + content, "target")
                            }

                        } else {
                            alert('无相关类型文章！');
                        }
                    }
                });
            } else {
                alert("请输入内容")
            }
        },
        keydown: function(event) {
            if (event.keyCode == 13) {
                this.bigsearch();
            }
        }
    }
})